All files / web/src/app/api/euclid/progress/[playerId] route.ts

0% Statements 0/77
0% Branches 0/1
0% Functions 0/1
0% Lines 0/77

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78                                                                                                                                                           
import { NextResponse } from 'next/server'
import { withAuth } from '@/lib/auth/withAuth'
import { eq } from 'drizzle-orm'
import { db } from '@/db'
import { euclidProgress } from '@/db/schema/euclid-progress'
import { canPerformAction } from '@/lib/classroom'
import { getUserId } from '@/lib/viewer'

/**
 * GET - Fetch completed proposition IDs for a player
 */
export const GET = withAuth(async (_request, { params }) => {
  try {
    const { playerId } = (await params) as { playerId: string }
    if (!playerId) {
      return NextResponse.json({ error: 'Player ID required' }, { status: 400 })
    }

    const userId = await getUserId()
    const canView = await canPerformAction(userId, playerId, 'view')
    if (!canView) {
      return NextResponse.json({ error: 'Not authorized' }, { status: 403 })
    }

    const rows = await db
      .select({ propositionId: euclidProgress.propositionId })
      .from(euclidProgress)
      .where(eq(euclidProgress.playerId, playerId))

    const completed = rows.map((r) => r.propositionId)

    return NextResponse.json({ completed })
  } catch (error) {
    console.error('Error fetching euclid progress:', error)
    return NextResponse.json({ error: 'Failed to fetch progress' }, { status: 500 })
  }
})

/**
 * POST - Mark a proposition as completed (idempotent upsert)
 */
export const POST = withAuth(async (request, { params }) => {
  try {
    const { playerId } = (await params) as { playerId: string }
    if (!playerId) {
      return NextResponse.json({ error: 'Player ID required' }, { status: 400 })
    }

    const userId = await getUserId()
    const canAct = await canPerformAction(userId, playerId, 'start-session')
    if (!canAct) {
      return NextResponse.json({ error: 'Not authorized' }, { status: 403 })
    }

    const body = await request.json()
    const propositionId = body.propositionId
    if (typeof propositionId !== 'number' || propositionId < 1 || propositionId > 48) {
      return NextResponse.json({ error: 'Invalid propositionId (must be 1-48)' }, { status: 400 })
    }

    // Idempotent insert — ignore if already exists
    await db.insert(euclidProgress).values({ playerId, propositionId }).onConflictDoNothing()

    // Return full updated list
    const rows = await db
      .select({ propositionId: euclidProgress.propositionId })
      .from(euclidProgress)
      .where(eq(euclidProgress.playerId, playerId))

    const completed = rows.map((r) => r.propositionId)

    return NextResponse.json({ completed })
  } catch (error) {
    console.error('Error marking euclid progress:', error)
    return NextResponse.json({ error: 'Failed to save progress' }, { status: 500 })
  }
})